package com.eleadmin.store.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.store.entity.ProductStorage;
import com.eleadmin.store.service.ProductStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 库存
 * @author : Doman
 * @date : 2024-02-13 18:44:18
 * @version ：1.0
 */

@Api(tags = "库存")
@RestController
@RequestMapping("/api/store/productStorage")
public class ProductStorageController extends BaseController {

    @Autowired
    private ProductStorageService productStorageService;

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("分页查询库存")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, ProductStorage productStorage) throws Exception{
        return productStorageService.list(page, productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("分页查询库存")
    @GetMapping("/pageNoStorageNums")
    public JsonResult pageNoStorageNums(PageResult2<T> page, ProductStorage productStorage) throws Exception{
        return productStorageService.pageNoStorageNums(page, productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("分页查询库存")
    @GetMapping("/storageNums")
    public JsonResult storageNums(ProductStorage productStorage) throws Exception{
        return productStorageService.storageNums(productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("查询全部库存")
    @GetMapping()
    public JsonResult list(ProductStorage productStorage){
        return productStorageService.list(productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    @ApiOperation("返回select列表")
    @GetMapping("listSel")
    public JsonResult listSel(ProductStorage productStorage){
        return productStorageService.listSel(productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("根据id查询库存")
    @GetMapping("productStorageNum")
    public JsonResult productStorageNum(ProductStorage productStorage) {
        return productStorageService.productStorageNum(productStorage, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:productStorage:list')")
    //@ApiOperation("根据id查询库存")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return productStorageService.getById(id);
    }


    @PreAuthorize("hasAuthority('store:productStorage:save')")
    @OperationLog
    @ApiOperation(value ="添加库存")
    @PostMapping()
    public JsonResult save(@RequestBody ProductStorage productStorage) {
        try {
            return productStorageService.save(productStorage, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('store:productStorage:update')")
    @OperationLog
    @ApiOperation(value ="修改库存")
    @PutMapping()
    public JsonResult update(@RequestBody ProductStorage productStorage) {
        try {
            return productStorageService.update(productStorage, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:productStorage:remove')")
    @OperationLog
    @ApiOperation(value ="删除库存")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return productStorageService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
